using System.Text;

namespace ICSharpCode.TextEditor.Formatting.TSqlFormatter
{
    public static class Utils
    {
        public static string HtmlEncode(string raw)
        {
            if (raw == null)
            {
                return null;
            }
            StringBuilder stringBuilder = null;
            int num = 0;
            int num2 = 0;
            foreach (char c in raw)
            {
                string text = null;
                switch (c)
                {
                    case '>':
                        text = "&gt;";
                        break;
                    case '<':
                        text = "&lt;";
                        break;
                    case '&':
                        text = "&amp;";
                        break;
                    case '"':
                        text = "&quot;";
                        break;
                }
                if (text != null)
                {
                    if (stringBuilder == null)
                    {
                        stringBuilder = new StringBuilder(raw.Length);
                    }
                    if (num2 < num)
                    {
                        stringBuilder.Append(raw.Substring(num2, num - num2));
                    }
                    stringBuilder.Append(text);
                    num2 = num + 1;
                }
                num++;
            }
            if (stringBuilder != null)
            {
                if (num2 < num)
                {
                    stringBuilder.Append(raw.Substring(num2));
                }
                return stringBuilder.ToString();
            }
            return raw;
        }
    }
}
